package cn.com.sparknet.jsindustrymanager;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.com.sparknet.jsindustrymanager.provider.DBManager;
import cn.com.sparknet.jsindustrymanager.provider.InputDatabaseInvalidException;
import cn.com.sparknet.jsindustrymanager.provider.InputNode;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.fragment.ViewExistsInAttachFragment;
import cn.emagsoftware.util.LogManager;

public class InputViewFragment extends ViewExistsInAttachFragment
{

    public InputViewFragment()
    {
    }

    @Override
    public View onCreateViewImpl(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreateViewImpl(inflater, container, savedInstanceState);
        Bundle bundle = getArguments();
        final String type = bundle.getString("type");
        InputNode node = (InputNode) bundle.getSerializable("node");
        View view = inflater.inflate(R.layout.inputview_fragment, null);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        TextView title = (TextView) view.findViewById(R.id.title);
        TextView content = (TextView) view.findViewById(R.id.content);
        TextView children = (TextView) view.findViewById(R.id.children);
        if (node.title.equals(""))
            title.setVisibility(View.GONE);
        else
            title.setText(node.title);
        if (node.content.equals(""))
            content.setVisibility(View.GONE);
        else
            content.setText(node.content);
        int maxIndex = node.children.size() - 1;
        if (maxIndex < 0)
            children.setVisibility(View.GONE);
        else
        {
            StringBuffer htmlBuffer = new StringBuffer();
            int index = -1;
            for (String[] cur : node.children)
            {
                htmlBuffer.append("<a href=\"" + cur[0] + "\">" + cur[1] + "</a>");
                if (++index < maxIndex)
                    htmlBuffer.append("<br><br>");
            }
            String html = htmlBuffer.toString();
            Spannable spannable = (Spannable) Html.fromHtml(html);
            URLSpan[] urls = spannable.getSpans(0, html.length(), URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(spannable.toString());
            style.clearSpans();// should clear old spans
            for (final URLSpan url : urls)
            {
                style.setSpan(new ClickableSpan()
                {
                    @Override
                    public void onClick(View widget)
                    {
                        // TODO Auto-generated method stub
                        String id = url.getURL();
                        InputNode node = null;
                        try
                        {
                            if ("FLFG".equals(type))
                                node = DBManager.queryFLFGInputNode(getActivity(), id);
                            else
                                node = DBManager.queryXCSCInputNode(getActivity(), id);
                            if (node == null)
                            {
                                ToastManager.showLong(getActivity(), R.string.inputview_inputdb_empty);
                                return;
                            }
                        } catch (InputDatabaseInvalidException e)
                        {
                            LogManager.logE(InputViewFragment.class, "input database is invalid.", e);
                            ToastManager.showLong(getActivity(), R.string.inputview_inputdb_invalid);
                            return;
                        }
                        FragmentTransaction transaction = getFragmentManager().beginTransaction();
                        transaction.addToBackStack(null);
                        InputViewFragment subInputFragment = new InputViewFragment();
                        Bundle subInputBundle = new Bundle();
                        subInputBundle.putString("type", type);
                        subInputBundle.putSerializable("node", node);
                        subInputFragment.setArguments(subInputBundle);
                        transaction.replace(R.id.content, subInputFragment).commit();
                    }
                }, spannable.getSpanStart(url), spannable.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            children.setText(style);
            children.setMovementMethod(LinkMovementMethod.getInstance());
        }
        return view;
    }

}
